package mammuthus.template.hadoop

import mammuthus.template.TemplateProcessor
import mammuthus.protocal.WriteFileTaskResponse
import mammuthus.tool.{Constants, TemplateTool}
import mammuthus.master.Master

/**
 * 5/5/15 WilliamZhu(allwefantasy@gmail.com)
 */
class YarnSiteProcessor(master: Master) extends TemplateProcessor {


  override def process(fileName: String, targetFilePath: String, params: Map[String, AnyRef]): List[WriteFileTaskResponse] = {
    val appName = params("appName").asInstanceOf[String]
    val app = params.getOrElse("app",appName)
    val hadoopInstallPath = master.appInstallPath+"/"+appName
    val nameNodesStr = params("nameNodes").asInstanceOf[String]
    val yarnResourceManagerNode = nameNodesStr.split(",")(0)

    val slaves = params.getOrElse("slaves",master.appInstallHosts(appName)).asInstanceOf[String]

    master.appPutExtra(Map("yarnResourceManager"->yarnResourceManagerNode))

    val configStr = TemplateTool.render(s"/template/app/$app/$fileName", Map(
      "nameNode" -> yarnResourceManagerNode,
      "hadoopInstallPath" -> hadoopInstallPath
    ))

    master.writeFile(slaves, configStr, targetFilePath, false)
  }
}
